/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: send.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月06日 星期二 11时27分24秒
***************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/shm.h>

typedef struct use{
	int age;
	char name[10];
}USE;
int main(int argc,const char* argv[])
{
	key_t key = 0;
	int shmid = 0;
	int ret = 0;

	key = ftok("./",1);
	if(key == -1)
	{
		perror("ftok");
		exit(-1);
	}

	shmid = shmget(key,1024,IPC_CREAT|0600);
	if(shmid == -1)
	{
		perror("shmget");
		exit(-1);
	}
	USE * str = NULL;
	str = shmat(shmid, NULL, 0);
    	if (str == (void *)-1) {
        	perror("shmat");
        	exit(-1);
    	}
	USE p;
	printf("请输入年龄:");
	scanf("%d",&p.age);
	printf("请输入姓名:");
	scanf("%s",p.name);
	
	memcpy(str,&p,sizeof(USE));

	 ret = shmdt(str);
    	if (ret == -1) {
        	perror("shmdt");
        	exit(-1);
    	}
	sleep(10);
	ret = shmctl(shmid, IPC_RMID, NULL);
    	if (ret == -1) {
        	perror("shmctl");
        	exit(-1);
    	}	
    return 0;
}
